/**
 * Copyright (c) 2009 blurredVision
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
 * associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or 
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT 
 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT 
 * OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#include "StdAfx.h"
#include "Camera.h"
#include "core/core.h"

namespace directx {

Camera::Camera(void)
{	
}

Camera::~Camera(void)
{
	release();
}

void Camera::reset()
{
	// Get the d3dDevice -> see DXResource
	DXResource::reset();
}

void Camera::release()
{
}

void Camera::draw()
{
	// Set the view matrix
	pd3dDevice->SetTransform( D3DTS_VIEW, (D3DXMATRIX*) &matView[0] );

	//if (coreGetRenderer()->getCurrentCamera() != this) 
	{
		pd3dDevice->SetTransform( D3DTS_PROJECTION, (D3DXMATRIX*) &matProj[0]);
	}
}

void Camera::setupProjection(float fov, float aspect, float znear, float zfar)
{
	fFov = fov; 
	fZNear = znear; 
	fZFar = zfar;
	fAspect = aspect;
	D3DXMatrixPerspectiveFovLH( (D3DXMATRIX*) &matProj, fFov, fAspect, fZNear, fZFar );
}

void Camera::updateMatrix()
{
	// Have the view matrix move the view move us to a good vantage point
	D3DXMatrixLookAtLH((D3DXMATRIX*)  &matView[0], 
						&D3DXVECTOR3(&vPosition.x),	// Camera position
						&D3DXVECTOR3(&vTarget.x),	// Look-at point
						&D3DXVECTOR3(&vUpVec.x) );	// Up vector

	// For the projection matrix, we set up a perspective transform (which
	// transforms geometry from 3D view space to 2D viewport space, with
	// a perspective divide making objects smaller in the distance). To build
	// a perpsective transform, we need the field of view (1/4 pi is common),
	// the aspect ratio, and the near and far clipping planes (which define at
	// what distances geometry should be no longer be rendered).
	if (m_bOrtho) 
	{
			D3DXMatrixOrthoLH( (D3DXMATRIX*) &matProj[0], (float)coreGetRenderer()->getBackBufferSize().x, (float)coreGetRenderer()->getBackBufferSize().y, fZNear, fZFar );
	} else 
	{
			D3DXMatrixPerspectiveFovLH( (D3DXMATRIX*) &matProj[0], fFov, fAspect, fZNear, fZFar );
	}
}

void Camera::setClippingPlanes( float zNear, float zFar)
{
	if (m_bOrtho) 
	{
			D3DXMatrixOrthoLH( (D3DXMATRIX*) &matProj[0], (float)coreGetRenderer()->getBackBufferSize().x, (float)coreGetRenderer()->getBackBufferSize().y, zNear, zFar );
	} else 
	{
			D3DXMatrixPerspectiveFovLH( (D3DXMATRIX*) &matProj[0], fFov, fAspect, zNear, zFar );
	}
}

} // namespace